package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	@Override
	public String submitRecharge(RechargeInfoVo rechargeInfoVo, Long userId) {
		//	1. 添加充值信息 recharge_info
		RechargeInfo rechargeInfo = new RechargeInfo();
		rechargeInfo.setUserId(userId);
		//	生成一个order_no;
		String orderNo = UUID.randomUUID().toString().replaceAll("-", "");
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfoMapper.insert(rechargeInfo);
		//	2. 让页面再次 调用微信支付接口 /createJsapi/{paymentType}/{orderNo}
		//	3. 支付成功：更新当前用户余额
		//	4. 记录用户充值信息
		return orderNo;
	}

	@Override
	public RechargeInfo getRechargeInfo(String orderNo) {
		//	调用mapper
		return rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo,orderNo));
	}
}
